﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class VisitRecordEdit : PageBase
    {
        #region 页面属性
        public string EditorContent
        {
            get
            {
                string id = GetQueryIntValue("id");
                if (!string.IsNullOrEmpty(id))
                {
                    PartyGrassrootsWorkTrend work = DB.PartyGrassrootsWorkTrends.Find(id);
                    return work.Content;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                //return "GrassrootsGroupAdd";
                return "GVisitRecordAdd";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBind(ddlGroup, PartyGrassrootsGroup.GetGroupData("", IsSystemAdmin ? "" : this.BranchID), "GroupName", "ID", true);
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsWorkTrend trend = DB.PartyGrassrootsWorkTrends.Find(id);
                if (trend == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                this.ddlGroup.SelectedValue = PartyGrassrootsGroupItem.QueryGroupIDByItemID(trend.ItemID);
                ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ItemID");
                ddlItem.SelectedValue = trend.ItemID;
                txtTitle.Text = trend.Title;
                cbStatus.Checked = trend.Status == 4;
                cbFlags.Checked = trend.Flags == 1;
                dpWorkTime.Text = Convert.ToDateTime(trend.WorkTime).ToString("yyyy-MM-dd");
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                PartyGrassrootsWorkTrend trend = DB.PartyGrassrootsWorkTrends.Find(id);
                if (trend == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                trend.ItemID = ddlItem.SelectedValue;
                trend.Title = txtTitle.Text.Trim();
                trend.Status = cbStatus.Checked ? 4 : 1;
                trend.Flags = cbFlags.Checked ? 1 : 0;
                trend.WorkTime = Convert.ToDateTime(dpWorkTime.Text);
                trend.Content = this.Request.Form["editorValue"];
                trend.ModifyTime = DateTime.Now;
                trend.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyGrassrootsWorkTrend trend = new PartyGrassrootsWorkTrend();
                trend.ID = StringHelper.NewID;
                trend.ItemID = ddlItem.SelectedValue;
                trend.Title = txtTitle.Text.Trim();
                trend.Status = cbStatus.Checked ? 4 : 1;
                trend.Flags = cbFlags.Checked ? 1 : 0;
                trend.WorkTime = Convert.ToDateTime(dpWorkTime.Text);
                trend.Content = this.Request.Form["editorValue"];
                trend.CreateTime = DateTime.Now;
                trend.ModifyTime = DateTime.Now;
                trend.CreateBy = CurrentUser.LoginID;
                trend.ModifyBy = CurrentUser.LoginID;

                DB.PartyGrassrootsWorkTrends.Add(trend);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ID", true);
        }
        #endregion
    }
}